<template>
  <v-dialog
    :title="title"
    :close-on-click-modal="false"
    v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'" label-width="120px">
      <el-row  >
        <!-- <el-col :span="12">
            <el-form-item label="合同" prop="contract.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择合同"
            labelName = 'contractName'
            labelValue = 'id'
            v-model = "inputForm.contract.id"
            :limit="1"
            :columns="[
            {
              prop: 'contractName',
              label: '合同名称'
            },
            {
              prop: 'contractNo',
              label: '合同编号'
            }
            ]"
            :searchs="[
            {
              prop: 'contractName',
              label: '合同名称'
            },
            {
              prop: 'contractNo',
              label: '合同编号'
            }
            ]"
            :api="tPurchaseContractService">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="采购外键" prop="purchase.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择采购外键"
            labelName = 'purchaseName'
            labelValue = 'id'
            v-model = "inputForm.purchase.id"
            :limit="1"
            :columns="[
            {
              prop: 'purchaseNo',
              label: '采购编号'
            },
            {
              prop: 'purchaseName',
              label: '采购名称'
            }
            ]"
            :searchs="[
            {
              prop: 'purchaseNo',
              label: '采购编号'
            },
            {
              prop: 'purchaseName',
              label: '采购名称'
            }
            ]"
            :api="purchaseService">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="项目外键" prop="project.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择项目外键"
            labelName = 'projectName'
            labelValue = 'id'
            v-model = "inputForm.project.id"
            :limit="1"
            :columns="[
            {
              prop: 'projectNo',
              label: '项目编号'
            },
            {
              prop: 'projectName',
              label: '项目名称'
            }
            ]"
            :searchs="[
            {
              prop: 'projectNo',
              label: '项目编号'
            },
            {
              prop: 'projectName',
              label: '项目名称'
            }
            ]"
            :api="projectMainService">
          </GridSelect>
           </el-form-item>
        </el-col> -->
        <el-col :span="24">
            <el-form-item label="付款时间" prop="paymentTime"
                :rules="[{required: true, message:'付款时间不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.paymentTime"
                      type="datetime"
                      value-format="YYYY-MM-DD HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="付款金额" prop="paymentAmount"
                :rules="[{required: true, message:'付款金额不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.paymentAmount" placeholder="请填写付款金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="备注" prop="remark"
                :rules="[
                 ]">
              <el-input v-model="inputForm.remark" placeholder="请填写备注" type='textarea' rows="6"    ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <template #footer>    
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="circle-close">关闭</el-button>
          <el-button  type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
        </span>
    </template>    
  </v-dialog>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import purchasePaymentService from '@/api/purchasepayment/purchasePaymentService'
  import tPurchaseContractService from '@/api/contract/tPurchaseContractService'   
  import purchaseService from '@/api/purchase/purchaseService'   
  import projectMainService from '@/api/project/projectMainService'   
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        tPurchaseContractService: tPurchaseContractService,  
        purchaseService: purchaseService,  
        projectMainService: projectMainService,  
        inputForm: {
          id: '',
          contract: {
            id: ''
          },
          purchase: {
            id: ''
          },
          project: {
            id: ''
          },
          paymentTime: '',
          paymentAmount: '',
          remark: ''
        }
      }
    },
    components: {
      GridSelect
    },
    created () {
    },
    methods: {
      init (method, id) {
        this.method = method
        
        if (method === 'add') {
          this.inputForm.id = '';
          this.inputForm.purchase.id = id.purchase.id;
        this.inputForm.contract.id = id.contract.id;
        this.inputForm.project.id = id.project.id;
          this.title = `新建采购付款`
        } else if (method === 'edit') {
          this.inputForm.id = id
          this.title = '修改采购付款'
        } else if (method === 'view') {
          this.inputForm.id = id
          this.title = '查看采购付款'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            purchasePaymentService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            purchasePaymentService.save(this.inputForm).then((data) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>

  
